package com.yd.app.mapper.provider;

import com.yd.domain.business.Message;
import com.yd.domain.searchParam.MessageParam;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zy
 * @create 2018-03-13 9:07
 * @desc 信息交流
 **/
public class MessageProvider extends BaseProvider{
    /**
     * @Author: zy
     * @Desc: 分页查询数据
     * @Date:  2018/3/13
     * @Param
     */
    public String searchMessageList(Map map){
        MessageParam messageParam= (MessageParam) map.get("messageParam");
        StringBuilder sb=new StringBuilder("SELECT m.id,m.content,m.publishTime,m.publishUser,u.realName as publishUserName," +
                " m.receiveUser,r.realName as receiveUserName,m.token,m.type FROM business_message m " +
                " LEFT JOIN sys_user u on u.id=m.publishUser " +
                " LEFT JOIN sys_user r on r.id=m.receiveUser " +
                "WHERE 1=1");
        sb.append(createSearchWhereSql(messageParam));
        sb.append("  limit #{startRow},#{pageSize}");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询数据条数
     * @Date:  2018/3/13
     */
    public String searchMessageCount(Map map){
        MessageParam messageParam= (MessageParam) map.get("messageParam");
        StringBuilder sb=new StringBuilder("SELECT COUNT(1) from business_message m  " +
                "  where 1=1 ");
        sb.append(createSearchWhereSql(messageParam));
        return sb.toString();
    }


    /**
     * @Author: zy
     * @Desc: 分页查询数据
     * @Date:  2018/3/13
     * @Param
     */
    public String searchMessageTalkList(Map map){
        StringBuilder sb=new StringBuilder("SELECT m.id,m.content,m.publishTime,m.publishUser,u.realName as publishUserName," +
                " m.receiveUser,r.realName as receiveUserName,m.token,m.type FROM business_message m " +
                " LEFT JOIN sys_user u on u.id=m.publishUser " +
                " LEFT JOIN sys_user r on r.id=m.receiveUser " +
                "WHERE 1=1 and  token=#{token} order by publishTime desc ");
        sb.append("  limit #{startRow},#{pageSize}");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询数据条数
     * @Date:  2018/3/13
     */
    public String searchMessageTalkCount(Map map){
        StringBuilder sb=new StringBuilder("SELECT COUNT(1) from business_message m  " +
                "  where 1=1 and token=#{token}  ");
        return sb.toString();
    }



    /**
     * @Author: zy
     * @Desc:查询条件
     * @Date:  2018/3/13
     */
    private String createSearchWhereSql(MessageParam messageParam){
        StringBuilder sb=new StringBuilder();
        if(!StringUtils.isEmpty(messageParam.getStartTime())){
            sb.append(" and m.publishTime >=#{messageParam.getStartTime}  ");
        }
        if(!StringUtils.isEmpty(messageParam.getEndTime())){
            sb.append(" and m.publishTime <=#{messageParam.getEndTime}  ");
        }
        if(!StringUtils.isEmpty(messageParam.getPublishUser())){
            sb.append(" and m.publishUser = '"+messageParam.getPublishUser()+"'  ");
        }
        if(!StringUtils.isEmpty(messageParam.getReceiveUser())){
            sb.append(" and m.receiveUser = '"+messageParam.getReceiveUser()+"'  ");
        }
        if(!StringUtils.isEmpty(messageParam.getToken())){
            sb.append(" and m.token = '"+messageParam.getToken()+"'  ");
        }
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc: 批量删除
     * @Date:  2018/3/13
     * @Param
     */
    public String deleteAll(Map map){
        Message message=(Message)map.get("message");
        StringBuilder sb=new StringBuilder("delete from business_message where id in (");
        sb.append(forAllMessage(message));
        sb.append(")");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc: id用逗号隔开
     * @Date:  2018/3/13
     * @Param
     */
    private String forAllMessage( Message message) {
        String arr[] = message.getStrMsg().split(",");
        StringBuilder sb = new StringBuilder("'" + arr[0] + "'");
        for (int i = 1; i < arr.length; i++) {
            sb.append(",'" + arr[i] + "' ");
        }
        return sb.toString();
    }
}
